package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.DefaultLayoutHelper;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.ApphomeModule1ViewBinding;
import sz.szsmk.citizencard.model.network.bean.response.QueryMainPageFunctionRes;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/12/20 14:04
 * description:
 */
public class AppHomeModule1Adapter extends DelegateAdapter.Adapter<AppHomeModule1Adapter.ApphomeModule1Holder> {

    private Context mContext;
    private LayoutHelper mGridLayoutHelper;
    private QueryMainPageFunctionRes.MainPageClassListBean viewData;
    private List<QueryMainPageFunctionRes.MainPageClassListBean.MainPageGroupListBean> main_page_group_list = new ArrayList<>();


    private BaseInterface baseInterface;

    public interface BaseInterface {
        public void onClick(View view, String url, String syslabl, int need_header, String modularId);
    }

    public void setBaseInterface(BaseInterface baseInterface) {
        this.baseInterface = baseInterface;
    }

    public AppHomeModule1Adapter(FragmentActivity activity, DefaultLayoutHelper headHelper) {
        mContext = activity;
        mGridLayoutHelper = headHelper;
    }


    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mGridLayoutHelper;
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.apphome_module1_view;
    }

    @Override
    public ApphomeModule1Holder onCreateViewHolder(ViewGroup parent, int viewType) {
//        View view = LayoutInflater.from(mContext).inflate(R.layout.apphome_module1_view, parent, false);
        return ApphomeModule1Holder.creat(parent);
    }

    @Override
    public void onBindViewHolder(ApphomeModule1Holder holder, int position) {
        loadItemData(holder.binding, position);
    }

    private void loadItemData(final ApphomeModule1ViewBinding binding, int position) {
        if (viewData != null) {
            RecyclerView moduleRv = binding.apphomeModule1Rv;
            GridLayoutManager manager = new GridLayoutManager(mContext, 2);
            moduleRv.setLayoutManager(manager);
            final ApphomeModuleItemAdapter itemAdapter = new ApphomeModuleItemAdapter(mContext);
            itemAdapter.setBaseInterface(new ApphomeModuleItemAdapter.BaseInterface() {
                @Override
                public void onClick(View view, String url, String syslabl, int need_header, String modularId) {
                    if (baseInterface!=null){
                        baseInterface.onClick(view,url,syslabl,need_header,modularId);
                    }
                }
            });
            moduleRv.setAdapter(itemAdapter);
        int index = DaggerApplication.getHmindx();
        switch (index){
            case 0:
                binding.line1.setVisibility(View.VISIBLE);
                binding.line2.setVisibility(View.GONE);

                binding.line3.setVisibility(View.GONE);
                itemAdapter.setViewData(main_page_group_list.get(0).getMain_page_function_list());
                itemAdapter.notifyDataSetChanged();
                break;

            case 1:
                binding.line1.setVisibility(View.GONE);
                binding.line2.setVisibility(View.VISIBLE);
                binding.line3.setVisibility(View.GONE);
                itemAdapter.setViewData(main_page_group_list.get(1).getMain_page_function_list());
                itemAdapter.notifyDataSetChanged();
                break;
            case 2:
                binding.line1.setVisibility(View.GONE);
                binding.line2.setVisibility(View.GONE);
                binding.line3.setVisibility(View.VISIBLE);
                itemAdapter.setViewData(main_page_group_list.get(2).getMain_page_function_list());
                itemAdapter.notifyDataSetChanged();
                break;
        }





            binding.apphomeModuleTitle.setText(viewData.getClass_name());
            binding.apphomeModuleTv1.setText(main_page_group_list.get(0).getGroup_name());
            binding.apphomeModuleTv2.setText(main_page_group_list.get(1).getGroup_name());
            binding.apphomeModuleTv3.setText(main_page_group_list.get(2).getGroup_name());



            binding.apphomeModuleTv1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    binding.line1.setVisibility(View.VISIBLE);
                    binding.line2.setVisibility(View.GONE);
                    binding.line3.setVisibility(View.GONE);
                    itemAdapter.setViewData(main_page_group_list.get(0).getMain_page_function_list());
                    itemAdapter.notifyDataSetChanged();
                    DaggerApplication.setHmindx(0);
                }
            });
            binding.apphomeModuleTv2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    binding.line1.setVisibility(View.GONE);
                    binding.line2.setVisibility(View.VISIBLE);
                    binding.line3.setVisibility(View.GONE);
                    itemAdapter.setViewData(main_page_group_list.get(1).getMain_page_function_list());
                    itemAdapter.notifyDataSetChanged();
                    DaggerApplication.setHmindx(1);
                }
            });
            binding.apphomeModuleTv3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    binding.line1.setVisibility(View.GONE);
                    binding.line2.setVisibility(View.GONE);
                    binding.line3.setVisibility(View.VISIBLE);
                    itemAdapter.setViewData(main_page_group_list.get(2).getMain_page_function_list());
                    itemAdapter.notifyDataSetChanged();
                    DaggerApplication.setHmindx(2);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    public void setViewData(QueryMainPageFunctionRes.MainPageClassListBean viewData) {
        this.viewData = viewData;
        if (viewData != null) {
            this.main_page_group_list = viewData.getMain_page_group_list();
        }
    }

    public static class ApphomeModule1Holder extends RecyclerView.ViewHolder {
        private ApphomeModule1ViewBinding binding;
        public static ApphomeModule1Holder creat(ViewGroup parent) {
            ApphomeModule1ViewBinding binding =
                    ApphomeModule1ViewBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new ApphomeModule1Holder(binding);
        }

        public ApphomeModule1Holder(ApphomeModule1ViewBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}
